public class Demo10 {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            //为什么isInterrupted判断后,会默认清除标志位,是因为一个线程中断一般来说只需要相应一次就足够了
            while (!Thread.currentThread().isInterrupted()) {
                System.out.println("hello thread!");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        thread.start();
        Thread.sleep(3000);
        System.out.println("终止线程!");
        //改变线程的中断标志位
        thread.interrupt();
    }
}
